package com.mz.admin.controller.common;


import com.github.pagehelper.PageHelper;
import com.mz.common.core.domain.AjaxResult;
import com.mz.common.core.domain.entity.SysDictData;
import com.mz.common.core.page.PageDomain;
import com.mz.common.utils.StringUtils;
import com.mz.framework.service.impl.SysDictDataServiceImpl;
import com.mz.framework.service.impl.SysDictTypeServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author zzq
 */
@Tag(name = "SysDictDataController", description = "数据字典信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/common/dict/data")
public class SysDictDataController {
    private final SysDictDataServiceImpl dictDataService;

    private final SysDictTypeServiceImpl dictTypeService;

    /**
     * 查询数据字典信息列表
     */
    @Operation(summary = "查询数据字典信息列表")
    @GetMapping("/list")
    public AjaxResult list(SysDictData dictData, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize());
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return AjaxResult.successPage(list);
    }

    /**
     * 查询字典数据详细
     */
    @Operation(summary = "查询字典数据详细")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode) {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @Operation(summary = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.successPage(data);
    }

    /**
     * 新增字典信息
     */
    @Operation(summary = "新增字典信息")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysDictData dict) {
        return AjaxResult.success(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类信息
     */
    @Operation(summary = "修改保存字典信息")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysDictData dict) {
        return AjaxResult.success(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典信息
     */
    @Operation(summary = "删除字典信息")
    @PostMapping("/remove/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return AjaxResult.success();
    }
}
